﻿# ----------------------------------------------- #
# Azure Resource Manager Resource Group Functions #
# ----------------------------------------------- #

# Add Resource Groups
function Add-ResourceGroups([System.Xml.XmlElement]$subscription)
{
	if (!(Should-Run($subscription.resourceGroups))) { return }
    try
    {
        Switch-AzureMode -Name AzureResourceManager
	    foreach ($resourceGroup in $subscription.resourceGroups.resourceGroup)
	    {
	        if (!(Should-Run($resourceGroup))) { continue }
		    Write-Information "Adding Resource Group '$($resourceGroup.name)' ... " -NoNewLine $true
	        if (Get-AzureResourceGroup | ? { $_.ResourceGroupName -eq $resourceGroup.name })
		    {
			    Write-Text "skipped."
		    }
		    else
		    {
		        $operation = New-AzureResourceGroup -Name $resourceGroup.name –Location $resourceGroup.location
		        if (!$? -or $operation.ProvisioningState -ne "Succeeded") { throw "Resource Group '$($resourceGroup.name)' operation failed." }
			    Write-Text "done."
		    }
	    }
    }
    finally
    {
        Get-Alias | ? {$_.ModuleName -eq "Azure"} | % { Remove-Item Alias:$($_.Name) }
        Switch-AzureMode -Name AzureServiceManagement
    }
}

# Remove Resource Groups
function Remove-ResourceGroups([System.Xml.XmlElement]$subscription)
{
	if (!(Should-Run($subscription.resourceGroups))) { return }
    try
    {
        Switch-AzureMode -Name AzureResourceManager
	    foreach ($resourceGroup in $subscription.resourceGroups.resourceGroup)
	    {
	        if (!(Should-Run($resourceGroup))) { continue }
	        if (Get-AzureResourceGroup | ? { $_.ResourceGroupName -eq $resourceGroup.name })
		    {
		        Write-Information "Removing Resource Group '$($resourceGroup.name)' ... " -NoNewLine $true
		        $operation = Remove-AzureResourceGroup -Name $resourceGroup.name -Force
		        if (!$?) { throw "Resource Group '$($resourceGroup.name)' operation failed." }
			    Write-Text "done."
		    }
		    else
		    {
		        Write-Information "Removing Resource Group '$($resourceGroup.name)' ... skipped."
		    }
	    }
    }
    finally
    {
        Get-Alias | ? {$_.ModuleName -eq "Azure"} | % { Remove-Item Alias:$($_.Name) }
        Switch-AzureMode -Name AzureServiceManagement
    }
}

# Move resource item
function Move-ResourceItem([string]$name, [string]$resourceType, [string]$resourceGroup)
{
    try
    {
        Switch-AzureMode -Name AzureResourceManager
        $resource = Get-AzureResource | ? { $_.Name -eq $name -and $_.ResourceType -eq $resourceType}
	    if ($resource.ResourceGroupName -ne $resourceGroup)
	    {
            do
            {
                $sourceState = (Get-AzureResourceGroup -Name $resource.ResourceGroupName).ProvisioningState
                $targetState = (Get-AzureResourceGroup -Name $resourceGroup).ProvisioningState
                Start-Sleep 3
            } while (($sourceState -eq "MovingResources") -or ($targetState -eq "MovingResources"))
            $operation = Move-AzureResource -ResourceId $resource.ResourceId -DestinationResourceGroupName $resourceGroup -Force
		    if (!$?) { throw "Resource '$name' operation failed." }
		    return $true
	    }
	    else
	    {
		    return $false
	    }
    }
    finally
    {
        Get-Alias | ? {$_.ModuleName -eq "Azure"} | % { Remove-Item Alias:$($_.Name) }
        Switch-AzureMode -Name AzureServiceManagement
    }
}